#include "shingles.h"


extern "C" __declspec(dllexport) bool GetDist(QList<Doc> &Docs,
                                              QVector<double> Params,
                                              QVector<QVector<double> >& Result)
{
    int shLength = (int) Params.at(0);
    int hashLength = (int) Params.at(1);
    //QVector<QVector<double> > Shingle;
    QVector<double> _Shingle;
    QList<QStringList> shingleList;
    QStringList _shingleList;
    QString _shingleStr;
    QList<Doc>::iterator it;
    for (it = Docs.begin(); it != Docs.end(); it++)
    {
        int length = Min(it->text.length(),shLength);
        for (int i = 0; i <= it->text.length() - length; i++)
        {
            _shingleStr = "";
            for (int j = 0; j < length; j++)
            {
                _shingleStr += it->text[j+i];
            }
            _shingleList.append(md5Hash(_shingleStr));
            //_shingleList.append(_shingleStr);
        }
        _shingleList.sort();
        if (_shingleList.count() > hashLength)
        for (int i = 0; i <= _shingleList.count() - hashLength; i++ ) _shingleList.removeLast();
        shingleList.append(_shingleList);
        _shingleList.clear();
    }

    QList<QStringList>::iterator listIt1;
    QList<QStringList>::iterator listIt2;
    QStringList::iterator strIt1;
    QStringList::iterator strIt2;
    int i = 0;
    for (listIt1 = shingleList.begin(); listIt1 != shingleList.end(); listIt1++)
    {
        for (listIt2 = shingleList.begin(); listIt2 != shingleList.end(); listIt2++)
        {
            i = 0;
            for (strIt1 = listIt1->begin(); strIt1 != listIt1->end(); strIt1++)
            {
                for (strIt2 = listIt2->begin(); strIt2 != listIt2->end(); strIt2++)
                {
                    if ( *strIt1 == *strIt2 )
                    {
                        i++;
                    }
                }
            }
            float ii = i;
            _Shingle.append(ii);
        }
        //Shingle.append(_Shingle);
        Result.append(_Shingle);
        _Shingle.clear();
    }
    //Result.append(Shingle); ;
    return true;
}


extern "C" __declspec(dllexport) bool GetParams(QList<Parameters> &Params)
{
    Parameters shLength;
    shLength.name = "shingle length";
    shLength.about = "abouth shingle length";
    shLength.type = 0;
    shLength.min = 1;
    shLength.max = 10;
    shLength.def = 2;
    Params.append(shLength);

    Parameters hashLength;
    hashLength.name = "hash length";
    hashLength.about = "abouth hash length";
    hashLength.type = 0;
    hashLength.min = 1;
    hashLength.max = 100;
    hashLength.def = 10;
    Params.append(hashLength);

    Parameters cb;
    cb.name = "minmax";
    cb.about = "minmax";
    cb.type = 1;
    cb.Items.append(QPair<QString,QString>("min","min"));
    cb.Items.append(QPair<QString,QString>("max","max"));
    cb.Items.append(QPair<QString,QString>("rand","rand"));
    Params.append(cb);
    return true;
}
